<?php
/*
 * 课程相关
 */
class CoursehourController extends BasicController {
    
    private function init(){
        Yaf_Registry::get('tokenPlugin')->checkAuth();
    }

    /*
    * 默认控制器 
    */
    public function indexAction(){
        echo '你好：'.G_USER_ID;
    }
    
        
    /*
     *	取出课时列表信息
     */
    public function listAction() {
    	$params = $this->getPost();
    	//章节id
    	$cc_id= isset($params['cc_id']) && !empty($params['cc_id']) ? $params['cc_id'] : '';
    	if(!$cc_id ){
    		$return['code'] = '3';
    		$return['msg'] = '非法参数';
    		$return['data'] = [];
    		echo json_encode($return);
    		return false;
    	}
    	
    	$where_data = [];
    	$where_data['isdelete']=0;
    	$where_data['cc_id']=$cc_id;
    	
    	$paging=isset($params['paging']) ? $params['paging']:0;
    	if($paging){
    		$page = max(1, intval($params['page']));
    		$perpage = $limit = isset($params['limit']) && !empty($params['limit']) ? $params['limit'] : '10';
    		$start=(($page-1) * $perpage);
    	}
    	   	   	  		   	   	              	 	
    	$orderby['ch_sort'] = " ASC";
    	$orderby['ch_id'] = " DESC ";
    	
    	$Course_hourModel = Helper::load('course_hour');
    	 
    	if($paging){
    		$page_result = $Course_hourModel->Where($where_data)->Order($orderby)->Limit($page,$perpage)->Select();
    	}else{
    		$page_result = $Course_hourModel->Where($where_data)->Order($orderby)->Select();
    	}
    	
    	if($page_result){
    		$return['code'] = 0;
    		$return['msg'] = '操作成功';
    		$return['data'] = $page_result;
    		echo json_encode($return);
    		return false;
    	
    	}else{
    		$return['code'] = 1;
    		$return['msg'] = '操作失败';
    		$return['data'] = [];
    		echo json_encode($return);
    		return false;
    	}
    
    }
    
    /*
     *	取出一条信息
     */
    public function detailAction() {
    	$params = $this->getPost();
    	$cm_id = isset($params['cm_id']) ? $params['cm_id'] : '';
    	if(!$cm_id){
    		$return['code'] = '3';
    		$return['msg'] = '非法参数';
    		$return['data'] = [];
    		echo json_encode($return);
    		return false;
    	}
    	$Course_mainModel = Helper::load('course_main');
    	$detail= $Course_mainModel->Where(["cm_id" => $cm_id])->SelectOne();
    	    	    
    	//DEBUG 调试输出
    	if($detail['is_group'] ==1){    		
    		$Course_mainModel = Helper::load('course_group_lesson');
    		$detail['child_course']= $Course_mainModel->Where(["cm_id" => $cm_id,'so_id'=>'-1'])->Select();    		  		    
    	}else{
    		$detail['child_course']=array();
    	}
    	if($detail){
    		$return['code'] = 0;
    		$return['msg'] = '操作成功';
    		$return['data'] = $detail;
    		echo json_encode($return);
    		return false;  		
    	}else{
    		$return['code'] = 1;
    		$return['msg'] = '操作失败';
    		$return['data'] = [];
    		echo json_encode($return);
    		return false;  		
    	}
    }
    
    /*
     *  添加课程
     */
    public function addAction(){
    	$params = $this->getPost();
    	$course_hour = isset($params['course_hour']) && !empty($params['course_hour']) ? $params['course_hour']:'';   
    	if(!$course_hour['cc_id']){
    		$return['code'] = '3';
    		$return['msg'] = '非法参数';
    		$return['data'] = [];
    		echo json_encode($return);
    		return false;
    	}
    	
    	$Course_chapterModel = Helper::load('course_chapter');
		$chapter= $Course_chapterModel->Where(["cc_id" =>$course_hour['cc_id']])->SelectOne();
		
		$Course_mainModel = Helper::load('course_main');
		$course_detail= $Course_mainModel->Where(["cm_id" => $chapter['cm_id']])->SelectOne();
		//判断是否为人机英语的课程
		if($course_detail['so_id']!='-1'){
			$return['code'] = '4';
			$return['msg'] = '没有权限';
			$return['data'] = [];
			echo json_encode($return);
			return false;
		}    	
    	//TODO 后端字段校验 根据具体业务逻辑添加
    	//DEBUG 判断是否数组字段,如果是数组转换为逗号间隔 字符串存贮
    	foreach($course_hour AS $key => $value){
    		if(is_array($value) && !empty($value)){
    			$course_hour[$key] = implode(",", $value);
    		}
    	}
    	$course_hour['create_dateline'] = CUR_TIMESTAMP;
    	$course_hour['modify_dateline'] = CUR_TIMESTAMP;
    	$course_hour['user_id'] = '-1';
    	
    	$Course_hourModel = Helper::load('course_hour');
    	$r = $Course_hourModel->Insert($course_hour);
    	if($r){  		
    		$hour= $Course_hourModel->Where(["ch_id" => $r])->SelectOne();
    		$return['code'] = 0;
    		$return['msg'] = '操作成功';
    		$return['data'] = $hour;
    		echo json_encode($return);
    		return false;
    	}else{
    		$return['code'] = 1;
    		$return['msg'] = '操作失败';
    		$return['data'] = [];
    		echo json_encode($return);
    		return false;
    	}    
    	   
    }
       
    /*
     *  编辑课时信息
     */
    public function editAction() {
    	$params = $this->getPost();
    	$course_hour = isset($params['course_hour']) && !empty($params['course_hour']) ? $params['course_hour']:'';
    	$ch_id = isset($params['ch_id']) ? $params['ch_id'] : '';   	
    	if(!$ch_id){
    		$return['code'] = '3';
    		$return['msg'] = '非法参数';
    		$return['data'] = [];
    		echo json_encode($return);
    		return false;
    	}
    	$Course_hourModel = Helper::load('course_hour');
    	$hour= $Course_hourModel->Where(["ch_id" =>$ch_id])->SelectOne();
    	
    	$Course_chapterModel = Helper::load('course_chapter');
    	$chapter= $Course_chapterModel->Where(["cc_id" =>$hour['cc_id']])->SelectOne();
    	
    	if(!$chapter['cm_id']){
    		$return['code'] = '3';
    		$return['msg'] = '非法参数';
    		$return['data'] = [];
    		echo json_encode($return);
    		return false;
    	}
    	
    	$Course_mainModel = Helper::load('course_main');
    	$course_detail= $Course_mainModel->Where(["cm_id" => $chapter['cm_id']])->SelectOne();
    	//判断是否为人机英语的课程
    	if($course_detail['so_id']!='-1'){
    		$return['code'] = '4';
    		$return['msg'] = '没有权限';
    		$return['data'] = [];
    		echo json_encode($return);
    		return false;
    	}
    	
    		//DEBUG 判断是否数组字段,如果是数组转换为逗号间隔 字符串存贮
    	foreach($course_hour AS $key => $value){
    		if(is_array($value) && !empty($value)){
    			$course_hour[$key] = implode(",", $value);
    		}
    	}
    	$course_hour['modify_dateline'] = CUR_TIMESTAMP;
    	$effect_row = $Course_hourModel->Where(['ch_id' => $ch_id])->UpdateOne($course_hour);
    	if($effect_row){
    		$hour= $Course_hourModel->Where(["ch_id" => $ch_id])->SelectOne();
    		$return['code'] = 0;
    		$return['msg'] = '操作成功';
    		$return['data'] = $hour;
    		echo json_encode($return);
    		return false;
    	}else{
    		$return['code'] = 1;
    		$return['msg'] = '操作失败';
    		$return['data'] = [];
    		echo json_encode($return);
    		return false;
    	}
    	
    }
    
    /*
     *  删除课时信息
     */
    public function deleteAction() {
    	$params = $this->getPost();
    	$ch_id = isset($params['ch_id']) ? $params['ch_id'] : '';
    	
    	if(!$ch_id){
    		$return['code'] = '3';
    		$return['msg'] = '非法参数';
    		$return['data'] = [];
    		echo json_encode($return);
    		return false;
    	}
    	
    	$Course_hourModel = Helper::load('course_hour');
    	$hour= $Course_hourModel->Where(["ch_id" =>$ch_id])->SelectOne();
    	 
    	$Course_chapterModel = Helper::load('course_chapter');
    	$chapter= $Course_chapterModel->Where(["cc_id" =>$hour['cc_id']])->SelectOne();
    	 
    	if(!$chapter['cm_id']){
    		$return['code'] = '3';
    		$return['msg'] = '非法参数';
    		$return['data'] = [];
    		echo json_encode($return);
    		return false;
    	}
    	 
    	$Course_mainModel = Helper::load('course_main');
    	$course_detail= $Course_mainModel->Where(["cm_id" => $chapter['cm_id']])->SelectOne();
    	//判断是否为人机英语的课程
    	if($course_detail['so_id']!='-1'){
    		$return['code'] = '4';
    		$return['msg'] = '没有权限';
    		$return['data'] = [];
    		echo json_encode($return);
    		return false;
    	}
    	
    	$course_hour['isdelete'] =1;
    	$course_hour['modify_dateline'] = CUR_TIMESTAMP;
    	
    	$effect_row = $Course_hourModel->Where(['ch_id' => $ch_id])->UpdateOne($course_hour);			
		if($effect_row){  	  		
    		$return['code'] = 0;
    		$return['msg'] = '操作成功';
    		$return['data'] = []; 		
    		echo json_encode($return);
    		return false;
    	}else{
   			$return['code'] = 1;
    		$return['msg'] = '操作失败';
    		$return['data'] = [];
	    	echo json_encode($return);
	    	return false;
    	}		
    	
    }
    
   
    
    
}